package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.ServiceObjTypeSelectAdapter;
import com.sx.zongzhi.bean.ServiceObjectTypeBean;
import com.sx.zongzhi.presenter.ServiceObjectTypePest;
import com.sx.zongzhi.utils.AppConstants;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IServiceObjectTypeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 服务对象类型选择
 */
public class ServiceObjectTypeActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener
        ,IServiceObjectTypeView {

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private ServiceObjTypeSelectAdapter selectAdapter;
    private List<ServiceObjectTypeBean> objectTypeBeanList=new ArrayList<>();
    private ServiceObjectTypePest objectPest;


    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.refresh_recyclerview);
        setTitleName("服务对象类型选择");
        ButterKnife.bind(this);
        initView();
        onRefresh();
    }

    private void initView() {
        objectPest=new ServiceObjectTypePest(this);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        selectAdapter = new ServiceObjTypeSelectAdapter(this,R.layout.item_event_type,objectTypeBeanList);
        selectAdapter.setEmptyView(View.inflate(this,R.layout.load_error_view,null));
        recyclerView.setAdapter(selectAdapter);
        selectAdapter.getObjectTypeBean(new ServiceObjTypeSelectAdapter.AdapterCallback() {
            @Override
            public void getData(ServiceObjectTypeBean typeBean, ServiceObjectTypeBean.SubTypesBean item) {
                Intent intent=new Intent();
                intent.putExtra("ServiceObjectTypeBean",typeBean);
                intent.putExtra("SubTypesBean",item);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    @Override
    public void onRefresh() {
        objectPest.getServiceObjectTypeList();
    }


    @Override
    public void showError(String message) {
        swipeRefreshLayout.setRefreshing(false);
        ToastUtils.showToast(this,message);
    }


    @Override
    public void getServiceObjectTypeList(List<ServiceObjectTypeBean> objectTypeBeen) {
        swipeRefreshLayout.setRefreshing(false);
        if (objectTypeBeen!=null&&objectTypeBeen.size()>0){
            objectTypeBeanList.clear();
            objectTypeBeanList.addAll(objectTypeBeen);
            selectAdapter.notifyDataSetChanged();
        }
    }
}
